package com.atguigu.file;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;

/**
 * className: FilesDemo
 * description:
 * date: 2023/3/10 0010-15:02
 * <p>
 * project: nio
 * package: com.atguigu.file
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class FilesDemo {

    public static void main(String[] args) {
        // createDirectory
        // Path path = Paths.get(Objects.requireNonNull(FilesDemo.class.getClassLoader().getResource("")).getPath().substring(1), "atguigutest");
        // try {
        //     Files.createDirectory(path);
        // } catch (IOException e) {
        //     e.printStackTrace();
        // }

        // Path path1 = Paths.get(Objects.requireNonNull(FilesDemo.class.getClassLoader().getResource("01.txt")).getPath().substring(1));
        // Path path2 = Paths.get(Objects.requireNonNull(FilesDemo.class.getClassLoader().getResource("atguigutest")).getPath().substring(1), "/001.txt");
        // try {
        //     Path path = Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);
        // } catch (IOException e) {
        //     e.printStackTrace();
        // }

        // Path sourcePath = Paths.get(Objects.requireNonNull(FilesDemo.class.getClassLoader().getResource("01.txt")).getPath().substring(1));
        // Path destinationPath = Paths.get(Objects.requireNonNull(FilesDemo.class.getClassLoader().getResource("")).getPath().substring(1), "01test.txt");
        //
        // try {
        //     Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
        // } catch (IOException e) {
        //     e.printStackTrace();
        // }

        // Path path = Paths.get(Objects.requireNonNull(FilesDemo.class.getClassLoader().getResource("001.txt")).getPath().substring(1));
        // try {
        //     Files.delete(path);
        // } catch (IOException e) {
        //     e.printStackTrace();
        // }

        Path rootPath = Paths.get(Objects.requireNonNull(FilesDemo.class.getClassLoader().getResource("")).getPath().substring(1));
        String fileToFind = File.separator + "001.txt";
        try {
            Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileString = file.toAbsolutePath().toString();

                    if (fileString.endsWith(fileToFind)) {
                        System.out.println("file found at path: " + file.toAbsolutePath());
                        return FileVisitResult.TERMINATE;
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
